package com.warm.doc.config;

import cn.hutool.core.util.StrUtil;
import com.github.xiaoymin.knife4j.solon.extension.OpenApiExtensionResolver;
import com.warm.core.utils.SolonUtil;
import lombok.Getter;
import lombok.Setter;
import org.noear.solon.Solon;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import org.noear.solon.core.BeanWrap;
import org.noear.solon.core.bean.LifecycleBean;
import org.noear.solon.docs.DocDocket;
import org.noear.solon.docs.models.ApiInfo;

/**
 * 接口文档配置类
 *
 * @author warm
 */
@Getter
@Setter
@Inject("${doc-config}")
@Configuration
public class DocConfig implements LifecycleBean {

    private String beanName;
    private String title;
    private String description;
    private String termsOfService;
    private String name;
    private String url;
    private String email;
    private String groupName;
    private String basePackage;

    @Inject
    OpenApiExtensionResolver openApiExtensionResolver;

    @Override
    public void start() {
        String groupName = StrUtil.emptyToDefault(getGroupName()
                , StrUtil.emptyToDefault(Solon.cfg().appGroup(), Solon.cfg().appName()));
        DocDocket docDocket = new DocDocket()
                .info(new ApiInfo().title(title).description(description).termsOfService(description)
                        .contact(name, url, email).version(SolonUtil.getProps("warm.version")))
                .globalResponseCodes(new HttpCodes()).groupName(groupName).apis(basePackage)
                .basicAuth(openApiExtensionResolver.getSetting().getBasic())
                .vendorExtensions(openApiExtensionResolver.buildExtensions());

        // 包装Bean（指定名字的）
        BeanWrap beanWrap = Solon.context().wrap(beanName, docDocket);

        // 以名字注册
        Solon.context().putWrap(beanName, beanWrap);
    }
}